package com.dhcc.core.modules.msgcenter.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.Condition;
import com.dhcc.core.framework.base.service.impl.BaseServiceImpl;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.msgcenter.dao.MsgUserSettingMapper;
import com.dhcc.core.modules.msgcenter.entity.MsgModule;
import com.dhcc.core.modules.msgcenter.entity.MsgUserSetting;
import com.dhcc.core.modules.msgcenter.service.IMsgModuleService;
import com.dhcc.core.modules.msgcenter.service.IMsgUserSettingService;

/**
 * 用户提醒设置表数据服务层接口
 * 
 * @ClassName: MsgUserSettingServiceImpl
 * @Description: TODO
 * @author cyf
 * @date 2018-11-15 15:02:16
 */
@Service("msgUserSettingService")
public class MsgUserSettingServiceImpl extends BaseServiceImpl<MsgUserSettingMapper, MsgUserSetting> implements IMsgUserSettingService {

    @Autowired
    private IMsgModuleService msgModuleService;
    @SuppressWarnings("unchecked")
    @Override
    @Transactional
    public List<MsgUserSetting> loadUserSetting(Long loginUserId) {
        List<MsgModule> modules = msgModuleService.selectList(null);
        List<MsgUserSetting> res = null;
        if(CommonUtil.isNotEmpty(modules)){
            res = new ArrayList<MsgUserSetting>();
            for (MsgModule m : modules) {
                MsgUserSetting setting = this.selectOne(Condition.create().eq("user_id", loginUserId).eq("module_id", m.getId()));
                if(CommonUtil.isEmpty(setting)){
                    setting = new MsgUserSetting();
                    setting.setModuleId(m.getId());
                    setting.setUserId(loginUserId);
                    setting.setPushTypes(m.getPushTypes());
                    setting.insert();
                }
                setting.setModuleName(m.getModuleName());
                res.add(setting);
            }
        }
        return res;
    }

}